package com.spt.springbootzhsx.service;

import com.spt.springbootzhsx.dao.CommentDao;
import com.spt.springbootzhsx.entity.Comment;
import com.spt.springbootzhsx.entity.back.CommentBack;
import com.spt.springbootzhsx.mapper.CommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.SimpleBeanInfo;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Service
public class CommentImpl implements CommentService{
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private CommentMapper commentMapper;

    @Override
    public String addComment(Comment comment) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        String time=simpleDateFormat.format(date);
        try {
            comment.setAddTime(simpleDateFormat.parse(time));
        } catch (ParseException e) {
            System.out.println("时间格式错误！");
            e.printStackTrace();
        }
        int k=commentMapper.addComment(comment);
        if(k>=1){
            return "评论发表成功！";
        }else{
            return "评论发表失败！";
        }
    }

    @Override
    public ArrayList<CommentBack> getAllCommentByMid(Integer mid) {
        ArrayList<CommentBack> list=commentDao.getCommentByMid(mid);
        return list;
    }

    @Override
    public String deleteComment(Integer coid) {
        int k=commentMapper.deleteComment(coid);
        if(k>=1){
            return "评论删除成功！";
        }else{
            return "评论删除失败！";
        }
    }

    @Override
    public ArrayList<CommentBack> getCommnetByUnameOrMname(String key) {
        ArrayList<CommentBack> ulist=commentDao.getCommentByUsername(key);
        if(ulist.size()>0){
            //如果是用户名搜索
            return ulist;
        }
        //如果是电影名搜索
        ArrayList<CommentBack> mlist=commentDao.getCommentByMname(key);
        return mlist;
    }

    @Override
    public ArrayList<CommentBack> getAllComment() {
        return commentDao.getAllComment();
    }
}
